<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card">
        <el-tabs v-model="activeName">

          <!-- 角色管理 -->
          <el-tab-pane label="角色管理" name="first">
            <!-- 新增角色 按钮 -->
            <el-button size="mini" type="primary" icon="el-icon-plus" @click="addRolesVisible = true">角色按钮</el-button>
            <el-table
              :data="roleData"
              style="width: 100%; margin: 15px 0;"
              border
            >
              <!-- label: 列标题 -->
              <!-- prop: 指定该列表渲染的数据字段 -->
              <el-table-column
                type="index"
                label="序号"
              />
              <el-table-column
                prop="name"
                label="角色名"
                sortable
              />
              <el-table-column
                prop="description"
                label="描述"
                sortable
              />
              <el-table-column
                label="操作"
              >
                <template slot-scope="{ row }">
                  <el-button type="text">分配权限</el-button>
                  <el-button type="text" @click="onEdit(row)">修改</el-button>
                  <el-button type="text">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <!-- 分页 -->
            <el-pagination
              :current-page="queryData.page"
              :page-sizes="[2, 5, 10, 20]"
              :page-size="queryData.pagesize"
              layout="total, sizes, prev, pager, next"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>

          <!-- 公司信息 -->
          <el-tab-pane label="公司信息" name="second">
            <el-row>
              <el-col :offset="3" :span="8">
                <el-form label-width="80px">
                  <el-form-item label="企业名称">
                    <el-input v-model="company.name" disabled />
                  </el-form-item>
                  <el-form-item label="公司地址">
                    <el-input v-model="company.companyAddress" disabled />
                  </el-form-item>
                  <el-form-item label="公司电话">
                    <el-input v-model="company.companyPhone" disabled />
                  </el-form-item>
                  <el-form-item label="邮箱">
                    <el-input v-model="company.mailbox" disabled />
                  </el-form-item>
                  <el-form-item label="备注">
                    <el-input v-model="company.remarks" disabled />
                  </el-form-item>
                </el-form>
              </el-col>
            </el-row>

          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>

    <!-- 新增角色弹层 -->
    <el-dialog
      title="新增角色"
      :visible.sync="addRolesVisible"
      width="30%"
      @close="addDialogClose"
    >
      <el-form ref="addRoleForm" :model="addRoleForm" :rules="addRoleRules" label-width="80px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="addRoleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="region">
          <el-input v-model="addRoleForm.region" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addRolesVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getCompanyInfo } from '@/api/company.js'
import { getRolesList, addRole, editRole } from '@/api/roles.js'
export default {
  name: 'Setting',
  data() {
    return {
      activeName: 'first',
      company: {},
      roleData: [],
      queryData: {
        page: 1, // 页码
        pagesize: 2 // 每页的条数
      },
      total: 0,
      addRolesVisible: false,
      addRoleForm: {
        name: '',
        region: ''
      },
      addRoleRules: {
        name: [
          { required: true, message: '角色名称不能为空!', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        region: [
          { required: true, message: '角色描述不能为空!', trigger: 'blur' },
          { min: 5, max: 20, message: '长度在 5 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },

  computed: {
    ...mapGetters(['companyId'])
  },

  created() {
    this.loadCompanyInfo()
    this.loadRolesList()
  },

  methods: {
    // 加载公司详情
    async loadCompanyInfo() {
      const res = await getCompanyInfo(this.companyId)
      this.company = res
    },
    // 加载角色列表
    async loadRolesList() {
      const res = await getRolesList(this.queryData)
      this.total = res.total
      this.roleData = res.rows
    },
    handleSizeChange(val) {
      this.queryData.pagesize = val
      this.loadRolesList()
    },
    handleCurrentChange(val) {
      // 改变参数page
      this.queryData.page = val
      this.loadRolesList()
    },
    // 重置表单 / 重置校验
    addDialogClose() {
      this.$refs.addRoleForm.resetFields()
      this.addRoleForm = {
        name: '',
        description: ''
      }
    },
    // 点击确定
    async onSave() {
      try {
        await this.$refs.addRoleForm.validate()
        // 表单校验
        // 修改
        if (this.addRoleForm.id) {
          // 如果有新增进行修改
          await editRole(this.addRoleForm)
          this.$message.success('更新成功')
        } else {
          await addRole(this.addRoleForm)
          this.$message.success('添加成功')
        }
        this.addRolesVisible = false
        this.loadRolesList()
      } catch (err) {
        console.log(err)
      }
    },
    // 点击修改
    onEdit(row) {
      this.addRolesVisible = true
      // 根据row.id 发送请求获取最新数据
      this.addRoleForm = { ...row }
    }
  }
}
</script>

<style scoped lang='scss'>
  ::v-deep .el-tabs__header {
    .el-tabs__item {
      width: 125px;
      text-align: center;
      font-size: 18px;
  }

}
.el-tab-pane {

  .el-form-item__label {
    font-weight: 400;
    background-color: red;
  }
}

</style>
